package com.mall.user.service.impl;

import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.strings.ErrorMsg;
import com.mall.constant.strings.SuccessMsg;
import com.mall.entity.TbUserBankCard;
import com.mall.user.dao.UserBankCardMapper;
import com.mall.user.service.UserBankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserBankCardServiceImpl implements UserBankCardService {

    @Autowired
    private UserBankCardMapper userBankCardMapper;

    /**
     * 绑定银行卡
     * 需要传入银行卡信息
     * @param userBankCard
     * @return
     */
    @Override
    public CommonResponse bindingCard(TbUserBankCard userBankCard) {
        //首先查询是否已经绑过该银行卡
        TbUserBankCard tbUserBankCard = userBankCardMapper.findByUidAndCardId(userBankCard.getUid(),userBankCard.getCardId());
        if (tbUserBankCard!=null){
            return new CommonResponse(CommonStatus.UNVALID.getCode(), ErrorMsg.ERROR_BANK_AGAIN_404);
        }else{
            userBankCard.setCreateTime(new Date());
            userBankCardMapper.insert(userBankCard);
            return new CommonResponse(CommonStatus.VALID.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }
    }

    @Override
    public void updateMoneyByCardId(TbUserBankCard userBankCard) {
        userBankCardMapper.updateMoneyByCardId(userBankCard);
    }
}
